<template>
  <div>
    <en-table-layout
      :tableData="tableData.data"
      :loading="loading"
    >
      <div slot="toolbar" class="inner-toolbar">
        <div class="toolbar-btns">
          <el-button size="mini" type="primary" icon="el-icon-circle-plus-outline" @click="handleAdd">添加</el-button>
        </div>
        <div class="toolbar-search">
          <en-table-search
            @search="searchEvent"
            @advancedSearch="advancedSearchEvent"
            advanced
            advancedWidth="465"
            placeholder="请输入活动名称"
          >
            <template slot="advanced-content">
              <el-form ref="advancedForm" :model="advancedForm" label-width="80px">
                <el-form-item label="活动状态">
                  <el-select v-model="advancedForm.status" placeholder="请选择" clearable>
                    <el-option label="未开始" value="NotStart"/>
                    <el-option label="进行中" value="Underway"/>
                    <el-option label="手动终止" value="Stop"/>
                    <el-option label="等待结算" value="WaitSettlement"/>
                    <el-option label="众筹成功" value="Success"/>
                    <el-option label="众筹失败" value="Fail"/>
                  </el-select>
                </el-form-item>
              </el-form>
            </template>
          </en-table-search>
        </div>
      </div>

      <template slot="table-columns">
        <el-table-column label="活动名称">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="众筹总金额">
          <template slot-scope="scope">￥{{ scope.row.total_price | unitPrice }}</template>
        </el-table-column>
        <el-table-column label="众筹成功比例">
          <template slot-scope="scope">{{ scope.row.price_ratio }}%</template>
        </el-table-column>
        <el-table-column label="开始时间">
          <template slot-scope="scope">{{ scope.row.start_time | unixToDate }}</template>
        </el-table-column>
        <el-table-column label="结束时间">
          <template slot-scope="scope">{{ scope.row.end_time | unixToDate }}</template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">{{ scope.row.status_text }}</template>
        </el-table-column>
        <el-table-column label="活动结束展示" key="end_show_flag">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.end_show_flag"
                       :active-value="true"
                       :inactive-value="false"
                       @change="handleUpdateEndShowFlag(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="price_percent" label="众筹进度">
          <template slot-scope="scope">{{ scope.row.price_percent }}%</template>
        </el-table-column>
        <el-table-column label="操作" min-width="200">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              v-if="scope.row.allowable.edit"
              @click="handleEdit(scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="primary"
              @click="handlerView(scope.row)">查看</el-button>
            <el-button
              v-if="scope.row.allowable.stop"
              size="mini"
              type="danger"
              @click="handleClose(scope.row)">终止</el-button>
            <el-button
              v-if="scope.row.allowable.delete"
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </template>

      <el-pagination
        v-if="tableData"
        slot="pagination"
        @size-change="handlePageSizeChange"
        @current-change="handlePageCurrentChange"
        :current-page="tableData.page_no"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="tableData.page_size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.data_total">
      </el-pagination>
    </en-table-layout>
  </div>
</template>

<script>
  import * as API_Crowdfunding from '@/api/crowdfunding'

  export default {
    name: 'crowdfunding',
    data() {
      return {
        /** 列表loading状态 */
        loading: false,
        /** 列表参数 */
        params: {
          page_no: 1,
          page_size: 10
        },

        /** 列表数据 */
        tableData: '',

        /** 高级搜索数据 */
        advancedForm: {}
      }
    },
    watch: {
      $route: {
        handler(val) {
          if (val.name === 'crowdfunding') {
            this.getList()
          }
        },
        immediate: true
      }
    },
    methods: {
      /** 分页大小发生改变 */
      handlePageSizeChange(size) {
        this.params.page_size = size
        this.getList()
      },

      /** 分页页数发生改变 */
      handlePageCurrentChange(page) {
        this.params.page_no = page
        this.getList()
      },

      /** 添加众筹活动 */
      handleAdd() {
        this.$router.push({
          name: 'addCrowdfunding'
        })
      },
      /** 查看活动详情 */
      handlerView(row) {
        this.$router.push({
          name: 'viewCrowdfunding',
          query: { id: row.id }
        })
      },

      /** 关闭活动 */
      handleClose(row) {
        this.$confirm('确认要终止吗？终止后将自动取消该活动的所有订单。', '提示').then(() => {
          API_Crowdfunding.stop(row.id).then(() => {
            this.$message.success('操作成功')
            this.getList()
          })
        }).catch(() => {})
      },
      /** 删除活动 */
      handleDelete(row) {
        this.$confirm('确认要删除这个活动吗？', '提示').then(() => {
          API_Crowdfunding.del(row.id).then(res => {
            this.$message.success('删除成功！')
            this.getList()
          })
        }).catch(() => {})
      },

      /** 修改活动 */
      handleEdit(row) {
        this.$router.push({
          name: 'editCrowdfunding',
          query: { id: row.id }
        })
      },

      /** 搜索事件触发 */
      searchEvent(data) {
        this.params.page_no = 1
        this.params.name = data
        if (!data) delete this.params.name
        this.getList()
      },

      /** 高级搜索事件触发 */
      advancedSearchEvent() {
        this.params = {
          ...this.params,
          ...this.advancedForm
        }
        this.params.page_no = 1
        this.getList()
      },

      /** 获取活动列表 */
      getList() {
        this.loading = true
        API_Crowdfunding.getList(this.params).then(response => {
          this.loading = false
          this.tableData = response
        }).catch(() => { this.loading = false })
      },
      /** 禁用/启用 */
      async handleUpdateEndShowFlag(row) {
        try {
          await API_Crowdfunding.updateEndShowFlag(row.id, row.end_show_flag)
          this.$message.success('操作成功！')
          // 刷新列表
          await this.getList()
        } catch (e) {
          // 取消或更新失败，恢复按钮
          row.end_show_flag = !row.end_show_flag
        }
      }
    }
  }
</script>
<style type="text/scss" lang="scss">
  .el-dialog__title {
    font-size:17px;
  }
</style>
